package main

import (
	"bluebell/controllers"
	"bluebell/dao/mysql"
	"bluebell/dao/redis"
	"bluebell/logger"
	snow "bluebell/pkg/sonwflake"
	"bluebell/routes"
	"bluebell/settings"
	"fmt"

	"go.uber.org/zap"
)

func main() {
	// 1. 加载配置文件
	if err := settings.Init(); err != nil {
		fmt.Printf("init settings failed, err:%v\n", err)
		return
	}
	// 2. 初始化日志
	if err := logger.Init(settings.Conf.LogConfig, settings.Conf.Mode); err != nil {
		fmt.Printf("init logger failed, err:%v\n", err)
		return
	}
	defer zap.L().Sync() // 异步缓冲写入文件
	// 3. 初始化mysql 链接
	if err := mysql.Init(settings.Conf.MySQLConfig); err != nil {
		fmt.Printf("init mysql failed, err:%v\n", err)
		return
	}
	defer mysql.Close()
	// 4. 初始化 redis链接
	if err := redis.Init(settings.Conf.RedisConfig); err != nil {
		fmt.Printf("init redis failed, err:%v\n", err)
		return
	}
	defer redis.Close()
	// 初始化雪花算法，生成id
	if err := snow.Init(settings.Conf.StartTime, settings.Conf.MachineID); err != nil {
		fmt.Printf("init snowflake failed, err:%v\n", err)
		return
	}
	// 注册验证器
	if err := controllers.InitTrans("zh"); err != nil {
		fmt.Printf("init validator trans failed, err:%v\n", err)
		return
	}
	// 5. 注册路由, 并初始化 engine 对象
	app := routes.Setup()
	// 6. 启动服务
	err := app.Run(fmt.Sprintf(":%d",
		settings.Conf.App.Port),
	)
	if err != nil {
		fmt.Printf("init app.Run failed, err:%v\n", err)
	}
}
